home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / 2D_3D / Tree3D_3.1 / Source / Branch.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  1.8 KB  |  71 lines

  1. // Branch.m -- major constituent of tree
  2.  
  3. #import "Branch.h"
  4. #import "Tree.h"
  5. #import "ForestCamera.h"
  6. #import <ri/ri.h>
  7. #import <math.h>
  8.  
  9. @implementation Branch:N3DShape
  10.  
  11. id globalShader;
  12. - tree :treeArg    {tree = treeArg;  return self;}
  13.  
  14. - addBranches :(int)level inTree :treeArg
  15. {    Branch *aBranch, *firstBranch=0;
  16.     Tree *myTree = tree;
  17.     RtPoint xAxis = {1.0, 0.0, 0.0};
  18.     RtPoint zAxis = {0.0, 0.0, 1.0};
  19.     int i, bf = [myTree randVal :BRANCHFACTOR];    // number of branches
  20.     float twi = [myTree randVal :TWIST];
  21.     
  22.     if (level >= (i=[myTree randVal :LEVELS]))
  23.         return self;
  24.         
  25.     for (i=0; i<bf; i++)
  26.     {    float shr = [myTree randVal :SHRINK];
  27.         float ang = [myTree randVal :ANGLE];
  28.         float tra = 0.90;                        // translation
  29.         static float k = 2.0*M_PI/360.0;
  30.         float x0=0.0, y0=0.0, z0=tra, x1, y1, z1;
  31.  
  32.         aBranch = [[Branch alloc] init];
  33.         if (i == 0)
  34.             [self linkDescendant :firstBranch=aBranch];
  35.         else
  36.             [firstBranch linkPeer :aBranch];
  37.         [aBranch tree :treeArg];
  38.         [aBranch setShader :globalShader];
  39.         [aBranch rotateAngle :ang axis :xAxis];
  40.         [aBranch rotateAngle :twi + 360.0*i/bf axis :zAxis];
  41.         [aBranch scaleUniformly :shr];
  42.         [aBranch translate :0.0 :0.0 :tra];
  43.  
  44.         if ((x1 = shr*sin(ang*k)*sin(twi*k)) < 0)    {    x0 = x1; x1 = 0.0;}
  45.         if ((y1 = shr*sin(ang*k)*cos(twi*k)) < 0)    {    y0 = y1; y1 = 0.0;}
  46.         if ((z1 = tra + shr*cos(ang*k)) < tra)        {    z0 = z1; z1 = tra;}
  47.         [aBranch setBoundingBox :x0 :x1 :y0 :y1 :z0 :z1];
  48.         [aBranch addBranches :level+1 inTree :treeArg];
  49.     }
  50.     return self;
  51. }
  52.  
  53. - setBoundingBox :(float)xMin :(float)xMax :(float)yMin :(float)yMax 
  54.                                                 :(float)zMin :(float)zMax
  55. {    boundingBox[0] = xMin;
  56.     boundingBox[1] = xMax;
  57.     boundingBox[2] = yMin;
  58.     boundingBox[3] = yMax;
  59.     boundingBox[4] = zMin;
  60.     boundingBox[5] = zMax;
  61.     return self;
  62. }
  63.  
  64. - renderSelf:(RtToken)context
  65. {    RiCylinder( .1, .1, 1, 360, RI_NULL);
  66.     return self;
  67. }
  68.  
  69. @end
  70.   
  71.